package de.wolvan.window;

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URISyntaxException;
import java.util.Iterator;

import de.wolvan.dataClient.WS;
import de.wolvan.main.main;
import de.wolvan.references.*;
import de.wolvan.tools.data;

public class SetPin extends JFrame implements ActionListener{

	private JButton add;
	private JButton back;
	
	private JLabel USERNAME;
	
	public static JTextField username;
	
	public static SetPin frame;
	
	public static void renderGUI() {
		frame = new SetPin("Set new PIN");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(390,180);
		frame.setLayout(null);
		frame.setVisible(true);
	}
	
	public SetPin(String title) {
		super(title);
		
		add = new JButton("Set Pin");
		add.setBounds(10,70,170,40);
		add.addActionListener(this);
		add(add);
		
		back = new JButton("Cancel");
		back.setBounds(190,70,170,40);
		back.addActionListener(this);
		add(back);
		
		USERNAME = new JLabel("New Pin (consists of 4 numbers):");
		USERNAME.setBounds(10,10,360,20);
		add(USERNAME);
		
		username = new JTextField("");
		username.setBounds(10,40,350,20);
		username.addActionListener(this);
		add(username);
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == back) {
			frame.setVisible(false);
			LoginWindow.frame.setVisible(true);
			LoginWindow.enableLogin();
		}
		if(e.getSource() == username) {
			add.doClick();
		}
		if(e.getSource() == add) {
			try {
				if(Integer.parseInt(username.getText()) < 10000 && Integer.parseInt(username.getText()) > 999 || username.getText().equals("0000")) {
					WS.comesFromSetPin = true;
					try {
						main.openWS();
					} catch (URISyntaxException e1) {
						e1.printStackTrace();
					}
					
					frame.setVisible(false);
				} else JOptionPane.showMessageDialog(null, "You have to set a PIN. The Pin consists of 4 NUMBERS.", "Please set a PIN", 0, null);
			
			} catch(NumberFormatException e1) {
				JOptionPane.showMessageDialog(null, "You have to set a PIN. The Pin consists of 4 NUMBERS.", "Please set a PIN", 0, null);
			}
		}
	}

	
}
